package com.fanly.weixin.utils;

import cn.hutool.core.util.StrUtil;
import com.fanly.weixin.entity.Index;
import com.fanly.weixin.entity.WeatherInfos;
import com.fanly.weixin.entity.WeatherOneInfos;
import com.fanly.weixin.entity.Weather_Data;
import com.google.gson.Gson;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

public class WeatherUtils {

	private static String findWeather(String httpUrl,String httpArg){
		BufferedReader reader = null;
		String result = "";
		StringBuffer buffer = new StringBuffer();
		httpUrl = httpUrl+"?"+httpArg;
		try{
			URL url = new URL(httpUrl);
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			connection.setRequestMethod("GET");
//			connection.setRequestProperty("apikey", getApiKey());
			connection.connect();
//			System.out.println("请求地址："+connection.getURL()+"\n"+connection.toString());
			InputStream is = connection.getInputStream();
			reader = new BufferedReader(new InputStreamReader(is, "utf-8"));
			String strRead = null;
			while((strRead=reader.readLine())!=null){
//				System.out.println(strRead);
				buffer.append(strRead);
				buffer.append("\r\n");
			}
			reader.close();
			//返回的数据为json
			result = buffer.toString();
			return result;
		}catch(Exception e){
			e.printStackTrace();
			return "";
		}
	}
	//开发者的apikey
	private static String getApiKey() {
		return "zzdU1WVvahIRMaFG3PY6dYVCk7gK3Dnd";
	}
	//获取天气数据
	public static String getWeather(String city){
		String httpUrl = "http://api.map.baidu.com/telematics/v3/weather";//"http://apis.baidu.com/lib360/weatherapi/weather360";
		String httpArg;
		try{
			httpArg = "location="+java.net.URLEncoder.encode(city,"utf-8")+"&output=json&ak="+getApiKey();
			String jsonResult = WeatherUtils.findWeather(httpUrl, httpArg);
//			System.out.println("返回的数据："+jsonResult);
			if(StrUtil.isEmpty(jsonResult)){
				return "";
			}else{
				//转换成对应的bean
				Gson gson = new Gson();
				WeatherInfos weatherInfos = gson.fromJson(jsonResult, WeatherInfos.class);
//				System.out.println(weatherInfos);
				StringBuffer buffer = new StringBuffer(city+"从"+weatherInfos.getDate()+"近四天的天气如下： \n");
				if("0".equals(weatherInfos.getError()) || "success".equals(weatherInfos.getStatus())){
					List<WeatherOneInfos> results = weatherInfos.getResults();
					//获取对应的数据： 日期 天气 气温
					for(int i=0;i<results.size();i++){
						WeatherOneInfos w = results.get(i);
						List<Weather_Data> weather_Datas = w.getWeather_data();
						List<Index> indexs = w.getIndex();
						//近四天的天气
						for(Weather_Data data :weather_Datas){
							buffer.append("    "+data.getDate()+","+data.getWeather()+","
									+data.getWind()+","+data.getTemperature()+"\n");
						}
						//生活指数
						buffer.append("\n生活指数： \n");
						for(Index index : indexs){
							buffer.append("    "+index.getTitle()+": "+index.getZs()+","+index.getDes()+"\n");
						}
					}
					return buffer.toString();
				}
			}
			
		}catch(Exception e){
			e.printStackTrace();
			return "";
		}
		return "";
	}
}
